package com.hejia.bulbul.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hejia.alauda.entity.AppVersion;
import com.hejia.alauda.service.ConfigureService;
import com.hejia.alauda.utils.Pager;

@Controller
@RequestMapping("appVersion")
public class AppVersionController extends BaseController {

	@Resource
	private ConfigureService appVersionService;

	/**
	 * app版本列表
	 */
	@RequestMapping(value = "versionlist", method = RequestMethod.GET)
	public String versionList(Pager<AppVersion> pager, ModelMap model) {
		Pager<AppVersion> appVersions = appVersionService
				.findAllAppVersion(pager);

		model.put("appVersions", appVersions);
		return "/app/appversion";
	}

	/**
	 * 跳转到添加界面
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addVersionList(ModelMap model) {

		return "/app/inputversion";
	}

	/**
	 * 添加完保存
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String saveVersionList(AppVersion appVersion, ModelMap model,
			RedirectAttributes redirectAttributes) {

		appVersion.setCreateDate(new Date());
		appVersion.setModifyDate(new Date());
//		appVersion.setIgnoreLogic("1==1?false:true");
		if (appVersionService.saveAppVersion(appVersion)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "App版本添加成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "App版本添加失败");
		}

		return "redirect:/appVersion/versionlist";
	}

	/**
	 * 跳转到修改界面
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateVersionList(@PathVariable Long id, ModelMap model) {
		AppVersion appversions = appVersionService.getAppVersionById(id);
		model.put("appversions", appversions);

		return "/app/inputversion";
	}

	/**
	 * 修改完保存
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.POST)
	public String saveUpdateVersionList(@PathVariable Long id,
			AppVersion appVersion, RedirectAttributes redirectAttributes) {

		AppVersion appVersion2 = appVersionService.getAppVersionById(id);
		appVersion2.setModifyDate(new Date());
		appVersion2.setType(appVersion.getType());
		appVersion2.setIgnoreLogic(appVersion.getIgnoreLogic());
		appVersion2.setVersion(appVersion.getVersion());
		appVersion2.setVersionName(appVersion.getVersionName());
		appVersion2.setUrl(appVersion.getUrl().trim());
		appVersion2.setMemo(appVersion.getMemo());

		if (appVersionService.modifyAppVersion(appVersion2)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "App版本修改成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "App版本修改失败");
		}

		return "redirect:/appVersion/versionlist";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteVersionList(@PathVariable Long id,
			RedirectAttributes redirectAttributes) {
		if (appVersionService.deleteAppVersion(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "App版本删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "App版本删除失败");
		}

		return "redirect:/appVersion/versionlist";
	}
}
